#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          SecoClientPromoteService
# Required-Start:    $local_fs $network
# Required-Stop:     
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: SecoClientPromoteService daemon
# Description:       SecoClientPromoteService daemon
### END INIT INFO


DAEMON=/usr/local/SecoClient/promote/SecoClientPromoteService
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#DAEMON=/home/build/dingyujun/SecoClient/promote/SecoClientPromoteService
SERVICEPATH=/usr/local/SecoClient/promote
SPESRESMON=/usr/local/spes/spesresmon
SPESRESMON2=spesresmon
NAME=SecoClientPromoteService
DESC="SecoClientPromoteService daemon"

test -x $DAEMON || exit 0

RUN=no
RUN_AS_USER=root
DAEMON_OPTS="-d"
OS=`cat /etc/*-release`
TARGET_TYPE="UNKNOWN"
case $OS in
*Ubuntu*|*ubuntu*) TARGET_TYPE="Ubuntu"
;;
*redhat*|*Redhat*) TARGET_TYPE="Redhat"
;;
*CentOS*|*centOS*) TARGET_TYPE="CentOS"
;;
esac


if [ "$TARGET_TYPE" = "Ubuntu" ] #ubuntu启动过程
then 
	case "$1" in
	start)
		echo -n "Starting $DESC: "
		start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		-d `dirname $DAEMON` --exec $DAEMON -- $DAEMON_OPTS
		echo "$NAME."
		;;
	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --oknodo --quiet --exec $DAEMON #$SPESRESMON
		pkill -9 $NAME 
		echo "$NAME."
		;;
	force-reload)
		# check whether $DAEMON is running. If so, restart
		start-stop-daemon --stop --test --quiet --pidfile \
			/var/run/$NAME.pid --exec $DAEMON \
		&& $0 restart \
		|| exit 0
		;;
	restart)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --oknodo --quiet --exec $SPESRESMON
		pkill -9 $NAEM 
		sleep 2
		start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.pid -d `dirname $DAEMON` --exec $DAEMON -- $DAEMON_OPTS
		echo "$NAME."
		;;
	status)
		if [ -s /var/run/$NAME.pid ]; then
				RUNNING=$(cat /var/run/$NAME.pid)
				if [ -d /proc/$RUNNING ]; then
					if [ $(readlink /proc/$RUNNING/exe) = $DAEMON ]; then
						echo "$NAME is running."
						exit 0
					fi
				fi

				# No such PID, or executables don't match
				echo "$NAME is not running, but pidfile existed."
				rm /var/run/$NAME.pid
				exit 1
			else
				rm -f /var/run/$NAME.pid
				echo "$NAME not running."
				exit 1
			fi
		;;
	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|force-reload}" >&2
		exit 1
		;;
	esac
	exit 0
else      #redhat启动过程
	case "$1" in
	start)
		echo -n "Starting $DESC: "
		cd $SERVICEPATH
		./$NAME &
		echo "$NAME."
		;;
	stop)
		echo -n "Stopping $DESC: "
		kill -9 $(ps -ef | grep SecoClientPromoteService | grep -v grep | awk '{print $2}')
		echo "$NAME."
		;;
	force-reload)
		# check whether $DAEMON is running. If so, restart
		kill -9 $(ps -ef | grep SecoClientPromoteService | grep -v grep | awk '{print $2}')\
		&& $0 restart \
		|| exit 0
		;;
	restart)
		echo -n "Restarting $DESC: "
		kill -9 $(ps -ef | grep SecoClientPromoteService | grep -v grep | awk '{print $2}') 
		sleep 2
		cd $SERVICEPATH
		./$NAME &
		echo "$NAME."
		;;
	status)
		if [ -s /var/run/$NAME.pid ]; then
				RUNNING=$(cat /var/run/$NAME.pid)
				if [ -d /proc/$RUNNING ]; then
					if [ $(readlink /proc/$RUNNING/exe) = $DAEMON ]; then
						echo "$NAME is running."
						exit 0
					fi
				fi

				# No such PID, or executables don't match
				echo "$NAME is not running, but pidfile existed."
				rm /var/run/$NAME.pid
				exit 1
			else
				rm -f /var/run/$NAME.pid
				echo "$NAME not running."
				exit 1
			fi
		;;
	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|force-reload}" >&2
		exit 1
		;;
	esac
	exit 0
fi

